package com.ticket.sass.admin.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ticket.sass.admin.dto.input.order.group.OrderGroupListInput;
import com.ticket.sass.admin.mapper.OrderGroupPurchaseMapper;
import com.ticket.sass.admin.service.BaseService;
import com.ticket.sass.common.entity.tenant.OrderGroupPurchaseEntity;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ywb
 * @since 2024-08-14
 */
@Service
public class OrderGroupPurchaseServiceImpl extends ServiceImpl<OrderGroupPurchaseMapper, OrderGroupPurchaseEntity> implements BaseService<OrderGroupPurchaseEntity> {
    
    public IPage<OrderGroupPurchaseEntity> iPage(OrderGroupListInput input) {
        // 创建 Page 对象
        Page<OrderGroupPurchaseEntity> page = new Page<>(input.getPageNum(), input.getPageSize());
        
        // 创建 QueryWrapper 对象
        QueryWrapper<OrderGroupPurchaseEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StrUtil.isNotEmpty(input.getOrderNo()), "o.order_no", input.getOrderNo());
        
        // 如果有效日期范围不为空，则添加范围查询条件
        if (input.getStartDate() != null && input.getEndDate() != null) {
            LocalDateTime startOfDay = input.getStartDate().atStartOfDay();
            LocalDateTime endOfDay = input.getEndDate().atTime(LocalTime.MAX);
            queryWrapper.between("o.created_at", startOfDay, endOfDay);
        }
        
        
        // 如果有效日期范围不为空，则添加范围查询条件
        if (input.getSessionStartDate() != null && input.getSessionEndDate() != null) {
            LocalDateTime startOfDay = input.getSessionStartDate().atStartOfDay();
            LocalDateTime endOfDay = input.getSessionEndDate().atTime(LocalTime.MAX);
            queryWrapper.between("s.start_time", startOfDay, endOfDay);
        }
        
        queryWrapper.in(input.getUserIds().size() > 0, "ot.user_id", input.getUserIds());
        queryWrapper.eq("o.type", 4);
        queryWrapper.notIn("o.pay_status", Arrays.asList(0, 1));
        queryWrapper.in(CollectionUtil.isNotEmpty(input.getPayStatusList()), "o.pay_status", input.getPayStatusList());
        queryWrapper.in(CollectionUtil.isNotEmpty(input.getPayTypes()), "o.pay_type", input.getPayTypes());
        
        
        queryWrapper.isNull("o.deleted_at");
        queryWrapper.isNull("ot.deleted_at");
        
        queryWrapper.orderByDesc("o.id");
        return this.baseMapper.queryPage(page, queryWrapper);
    }
    
    public List<OrderGroupPurchaseEntity> list(OrderGroupListInput input) {
        // 创建 QueryWrapper 对象
        QueryWrapper<OrderGroupPurchaseEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StrUtil.isNotEmpty(input.getOrderNo()), "o.order_no", input.getOrderNo());
        
        // 如果有效日期范围不为空，则添加范围查询条件
        if (input.getStartDate() != null && input.getEndDate() != null) {
            LocalDateTime startOfDay = input.getStartDate().atStartOfDay();
            LocalDateTime endOfDay = input.getEndDate().atTime(LocalTime.MAX);
            queryWrapper.between("o.created_at", startOfDay, endOfDay);
        }
        
        
        // 如果有效日期范围不为空，则添加范围查询条件
        if (input.getSessionStartDate() != null && input.getSessionEndDate() != null) {
            LocalDateTime startOfDay = input.getSessionStartDate().atStartOfDay();
            LocalDateTime endOfDay = input.getSessionEndDate().atTime(LocalTime.MAX);
            queryWrapper.between("s.start_time", startOfDay, endOfDay);
        }
        queryWrapper.in(CollectionUtil.isNotEmpty(input.getPayStatusList()), "o.pay_status", input.getPayStatusList());
        queryWrapper.in(CollectionUtil.isNotEmpty(input.getPayTypes()), "o.pay_type", input.getPayTypes());
        
        
        queryWrapper.in(input.getUserIds().size() > 0, "ot.user_id", input.getUserIds());
        queryWrapper.eq("o.type", 4);
        queryWrapper.notIn("o.pay_status", Arrays.asList(0, 1));
        queryWrapper.isNull("o.deleted_at");
        queryWrapper.isNull("ot.deleted_at");
        
        queryWrapper.orderByDesc("o.id");
        return this.baseMapper.queryList(queryWrapper);
    }
}
